package cn.lkx.crud.service;

import cn.lkx.crud.bean.Emp;
import cn.lkx.crud.bean.EmpExample;
import cn.lkx.crud.dao.EmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpService {
    @Autowired
    EmpMapper empMapper;
    //获取所有员工
    public List<Emp> getAll() {
        //带部门信息
        return empMapper.selectByExampleWithDept(null);
    }
    //员工保存
    public void savaEmp(Emp emp){
        empMapper.insertSelective(emp);
    }
    //检验用户名是否可用
    public boolean checkUser(String empName){
        EmpExample empExample = new EmpExample();
        EmpExample.Criteria criteria = empExample.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        int count = empMapper.countByExample(empExample);
        return count == 0;
    }
    //按id找员工
    public Emp getEmp(Integer id){
        Emp emp = empMapper.selectByPrimaryKey(id);
        return emp;
    }
    //员工更新
    public void updateEmp(Emp emp){
        empMapper.updateByPrimaryKeySelective(emp);
    }

    //员工删除
    public void deleteEmp(Integer id) {
        // TODO Auto-generated method stub
        empMapper.deleteByPrimaryKey(id);
    }
    public void deleteBatch(List<Integer> ids) {
        // TODO Auto-generated method stub
        EmpExample example = new EmpExample();
        EmpExample.Criteria criteria = example.createCriteria();
        //delete from xxx where emp_id in(1,2,3)
        criteria.andEmpIdIn(ids);
        empMapper.deleteByExample(example);
    }

}
